const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectDemands: [],
    remark: '',
    textNumber: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const selectDemandsJson = wx.getStorageSync("selectDemands");
    const selectDemands = JSON.parse(selectDemandsJson);
    const remark = wx.getStorageSync("remark") || '';
    this.setData({
      selectDemands: selectDemands,
      remark: remark
    });
  },
  firstPage: function() {
    wx.navigateBack({
      delta: 1
    })
  },
  bindTextArea: function(e) {
    this.setData({
      remark: e.detail.value
    });
  },
  saveDemand: function() {
    const url = app.globalData.url;
    const openId = app.globalData.openId;
    const selectDemands = this.data.selectDemands || [];
    const remark = this.data.remark;
    wx.request({
      url: url + '/train/savePlan',
      data: {
        openId: openId,
        trainDemand: selectDemands.join(','),
        remark: remark
      },
      method: 'POST',
      success: function(response) {
        if (response.data.success) {
          wx.navigateBack({
            delta: 3
          });
          wx.showToast({
            title: '保存成功',
            icon: 'none'
          })
        } else {
          wx.showToast({
            title: '保存失败:' + response.data.message,
            icon: 'none'
          })
        }
      }
    })
  }
})